<?php
/*
-- OVERVIEW
Functions to parse $_REQUEST parameters into arrays

-- RECCOMEND:
Keep and add additional JSON types (By list and All)

*/


/**
 * CF_ParamReciever class contains functions to fill variables and arrays with
 * passed parameter information from request values.
 *
 * @package CreateFast
 */

 class CF_ParamReciever {
 
	// return true if request recieved via ajax
 	static function GetReqIsAjax(){
		if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
			return true;
		}
		else{
			return false;
		}	
	}
	
	
	// Checked ok
	// Expects parameters and values to be passed in $_REQUEST ($_GET, $_POST, $_COOKIE) as &<parameter_n>=<value_n> form
	// prefix can be used, will be expected as prefix to parameter
	// return an array of named parameter and parameter value pairs
	// example: result[0]= array(<parameter1> => <parameter1_value>, <parameter2> => <parameter2_value>, ...)
	// results found at 0 index of holding array to be consistent with multiple set returning functions.
	// If requireall = true, only return if all parameters named in list array are found.
	static function GetReqParamsByList($list,$prefix = '',$requireall = false){
		$foundall = true;
		foreach ($list as $col_name){
			if((isset($_REQUEST[$prefix . $col_name])) AND ($_REQUEST[$prefix . $col_name] != '') ){
				$result[0][$col_name] = $_REQUEST[$prefix .$col_name];
				}else{
				   $foundall = false;
				}
			}
		if($requireall){
			if($foundall){
				return $result;
			}else{

				return false;
			}			
		}else{
			return $result;
		}
	}
		

		
	static function GetReqParamsAll($method = 'REQ'){
		$foundall = true;
		if($method == 'GET'){
			if(is_array($_GET)){
			foreach ($_GET AS $col_name=>$col_value){
				$result[0][$col_name] = $col_value;
			}
			}		
		}else if($method == 'POST'){
			if(is_array($_POST)){
			foreach ($_POST AS $col_name){
				$result[0][$col_name] = $_POST[$col_name];
			}
			}		
		}else{
			if(is_array($_REQUEST)){
			foreach ($_REQUEST AS $col_name=>$col_value){
				$result[0][$col_name] = $col_value;
			}
			}		
		}
		return $result;
	}		
		
	// Expects parameters and values to be passed in $_REQUEST ($_GET, $_POST, $_COOKIE)
	// as packed in a delimited string on a single parameter
	// of form:  &<ValParam>=<Value1>,<Value2>, .. <Valuen>&<VarParam>=<Param1>,<Param2>,..<Paramn>
	// return an array of named parameter and parameter value pairs
	// example: result[0]= array(<parameter1> => <parameter1_value>, <parameter2> => <parameter2_value>, ...)
	// results found at 0 index of holding array to be consistent with multiple set returning functions.
	static function GetReqParamsPacked($VarParam, $ValParam, $value_delimiter = ',', $VarsByRequest = false){
		if (isset($_REQUEST[$ValParam])) {
			$strValues = $_REQUEST[$ValParam];
			$arValues = explode($value_delimiter, $strValues);
			//$proceed = true;
			
			}
		else {
			//$proceed = false;
		}
		$ValueCount = count($arValues);
		if($VarsByRequest){
			if (isset($_REQUEST[$VarParam])) {
				$strVariables = $_REQUEST[$VarParam];
				$arVariables = explode($value_delimiter, $strVariables);
				}
			else {
				$strVariables = 0;
				$proceed = false;
				}
			}
		else {
				$strVariables = $VarParam;
				$arVariables = explode($value_delimiter, $strVariables);		
		}
		$ParamsCount = count($arVariables);
		// Assign the value for each key variable into the $keyvalues array (position 0 since only one row specified)
		if($ValueCount = $ParamsCount){
			foreach($arVariables AS $index=>$keyvar){
				$keyval = $arValues[$index];
				$resultline[$keyvar] = $keyval;
				//print "KEY $keyvar = $keyval \n";
			}
			$result[0] = $resultline;
			return $result;
		}				
	}
	
	//Checked OK
	// Gets a single value assigned to the parameter
	// Expectes it passed as a $_REQUEST (&<var>=<value>)
	static function GetSingleValue($varParam = 'var',$valParam = 'value'){
			$changecol = $_REQUEST[$varParam];
			$changevalue = $_REQUEST[$valParam];
			$result[0][$changecol] = $changevalue;
			return $result;
	}
	
	// Expects multiple sets of parameter values to be passed in $_REQUEST ($_GET, $_POST, $_COOKIE)
	// as packed in a delimited string on a single parameter
	// of form:  &<ValParam>=<Value1-1>,<Value1-2>, .. <Value1-n>;Value2-1>,<Value2-2>, .. <Value2-n>; ..Valuem-1>,<Valuem-2>, .. <Valuem-n>
	// &<VarParam>=<Param1>,<Param2>,..<Paramn>
	// return an array of named parameter and parameter value pairs
	// example: result[0]= array(<parameter1> => <parameter1_value>, <parameter2> => <parameter2_value>, ...)
	// results found at 0 index of holding array to be consistent with multiple set returning functions.
	// Note - need to add check that number of value and variables is saem
	static function GetPackedParamSets($VarParam, $SetParam, $value_delimiter = ',', $set_delimiter = ';',$VarsByRequest = false){
		$dochange = false;
		if(!empty($_REQUEST[$SetParam]) OR $_REQUEST[$SetParam] === 0){
			// Check for the variable order, in the comma list 'VarParam'
			if (isset($_REQUEST[$VarParam])) {
				$strVariables = $_REQUEST[$VarParam];
				$arVariables = explode($value_delimiter, $strVariables);
			}
			else {
				//$strVariables = 0;
				$arVariables = explode($value_delimiter, $VarParam);
			}	
			// produce an array ordering key variables
			foreach($arVariables AS $orderindex=>$keyvar){
				$keyorder[$orderindex] = $keyvar;
			}
			// split up sets of key values and assign to 
			$arSets = explode($set_delimiter, $_REQUEST[$SetParam]);
	        foreach($arSets AS $setindex=>$arTheSet){
				$arValues = explode($value_delimiter, $arTheSet);
	            foreach($arValues AS $keyindex=>$keyvalue){
						$keyval = $keyorder[$keyindex];
						$valueset[$keyval] = $keyvalue;
	                }
				$result[] = $valueset;	
	            }
				return $result;
		} // multikey element is not empty	
	}

	// For the case where multiple lines (identified by a key set) are filled to the same newvalues
	static function FillKeysToNewVals(&$keyvalues, &$newvalues){
		foreach($keyvalues AS $i->$thisline){
			$newvalues[$i] = $newvalues[0];
		}
	}

	
	// If any of the keys on TargetList are not in $LimitList, unset them.
	static function FilterByList(&$TargetList, $LimitList){
		$FlipList = array_flip($LimitList);
		foreach($TargetList AS $key => $item){
			if (!isset($FlipList[$key])){
				unset($TargetList[$key]);
			}
		}
	}
	// chck that all elements specified by $LimitList appear as keys in $TargetList
	function CheckArrayComplete($TargetList, $LimitList){
		foreach($LimitList AS $item){
			if(!isset($TargetList[$item])){
				return false;
			}
		}
		return true;
	}
	
		
 
 
 
 }

?>